package cn.csc.module.distribute.controller.admin.downline;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.distribute.controller.admin.downline.vo.*;
import cn.csc.module.distribute.controller.app.order.vo.AppDistributeOrderRespVO;
import cn.csc.module.distribute.convert.downline.DistributeDownlineConvert;
import cn.csc.module.distribute.dal.dataobject.downline.DistributeDownlineDO;
import cn.csc.module.distribute.service.downline.DistributeDownlineService;
import cn.csc.module.member.api.user.MemberUserApi;
import cn.csc.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 分销下线信息")
@RestController
@RequestMapping("/distribute/downline")
@Validated
public class DistributeDownlineController {

    @Resource
    private DistributeDownlineService downlineService;
    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @ApiOperation("创建分销下线信息")
    @PreAuthorize("@ss.hasPermission('distribute:downline:create')")
    public CommonResult<BigInteger> createDownline(@Valid @RequestBody DistributeDownlineCreateReqVO createReqVO) {
        return success(downlineService.createDownline(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新分销下线信息")
    @PreAuthorize("@ss.hasPermission('distribute:downline:update')")
    public CommonResult<Boolean> updateDownline(@Valid @RequestBody DistributeDownlineUpdateReqVO updateReqVO) {
        downlineService.updateDownline(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除分销下线信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:downline:delete')")
    public CommonResult<Boolean> deleteDownline(@RequestParam("id") BigInteger id) {
        downlineService.deleteDownline(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得分销下线信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('distribute:downline:query')")
    public CommonResult<DistributeDownlineRespVO> getDownline(@RequestParam("id") BigInteger id) {
        DistributeDownlineDO downline = downlineService.getDownline(id);
        return success(DistributeDownlineConvert.INSTANCE.convert(downline));
    }

    @GetMapping("/list")
    @ApiOperation("获得分销下线信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('distribute:downline:query')")
    public CommonResult<List<DistributeDownlineRespVO>> getDownlineList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<DistributeDownlineDO> list = downlineService.getDownlineList(ids);
        return success(DistributeDownlineConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得分销下线信息分页")
    @PreAuthorize("@ss.hasPermission('distribute:downline:query')")
    public CommonResult<PageResult<DistributeDownlineRespVO>> getDownlinePage(@Valid DistributeDownlinePageReqVO pageVO) {
        PageResult<DistributeDownlineRespVO> pageResult = downlineService.selectAdminPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出分销下线信息 Excel")
    @PreAuthorize("@ss.hasPermission('distribute:downline:export')")
    @OperateLog(type = EXPORT)
    public void exportDownlineExcel(@Valid DistributeDownlinePageReqVO pageVO,
              HttpServletResponse response) throws IOException {
        List<DistributeDownlineExcelVO> list = downlineService.getExportDownlineList(pageVO);
        // 导出 Excel
        ExcelUtils.write(response, "分销下线信息.xls", "数据", DistributeDownlineExcelVO.class, list);
    }

}
